package com.uikoo9.manage.y.model;

import java.util.List;
import java.util.Map;

import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Model;
import com.uikoo9.manage.y.util.SqlKit;
import com.uikoo9.plugin.QLogger1;
import com.uikoo9.util.core.annotation.QTable;
import com.uikoo9.util.core.data.QStringUtil;

/**
 * YIdeaModel<br>
 * id	id<br>
 * y_idea_name	名称<br>
 * y_idea_starttime	开始时间<br>
 * y_idea_endtime	截止时间<br>
 * y_idea_status	状态 1待处理 5 处理中 10完成 50取消<br>
 * y_idea_parentid	上级id<br>
 * y_idea_topid	顶级id<br>
 * y_idea_type	类型 1底层<br>
 * y_idea_version	版本<br>
 * y_project_id	项目id<br>
 * y_idea_level	级别 1-5<br>
 * cuser_name	input_name<br>
 * cdate	创建时间<br>
 * cuser_id	input_id<br>
 * @author qiaowenbin
 */
@QTable("t_y_idea")
@SuppressWarnings("serial")
public class YIdeaModel extends Model<YIdeaModel>{
	
	public static final YIdeaModel dao = new YIdeaModel();
	
	/**
	 * find all
	 * @return
	 */
	public List<YIdeaModel> findAll(){
		return findAll(null);
	}
	
	/**
	 * find all by order
	 * @param order
	 * @return
	 */
	public List<YIdeaModel> findAll(String order){
		StringBuilder sb = new StringBuilder("select * from t_y_idea ");
		if(QStringUtil.isEmpty(order)){
			return dao.find(sb.append("order by id desc").toString());
		}else{
			return dao.find(sb.append(order).toString());
		}
	}

    public List<YIdeaModel> find(Map<String,Object> param){
        return  dao.find(null,param);
    }

    public List<YIdeaModel> find(Map<String,Object[]> querytype,Map<String,Object> param){
        String sql="select i.* from t_y_idea i left join t_y_project p on i.y_project_id = p.id where 1=1";
        SqlKit s = new SqlKit();
        if(param!=null && !param.isEmpty()){
            if(querytype != null){
                s.buildSQL(querytype,param);
            }else {
                s.buildSQL(param);
            }
            sql+= s.getSql();
        }
        sql = s.appendRightSql(sql,"p","");
        QLogger1.getLogger().debug(sql);
        return  dao.find(sql,s.getParamArray());
    }

    public List<YIdeaModel> findByProjectId(int id){
        return  YIdeaModel.dao.find("select * from t_y_idea where y_project_id = ?",id);
    }

    public List<YIdeaModel> findByLastId(){
        return  YIdeaModel.dao.find("select a.* from t_y_idea a where 1=1 and a.id not in (select y_idea_parentid from t_y_idea where y_idea_parentid is not null )");
    }

    public List<YIdeaModel> findTeamline(){
        return  YIdeaModel.dao.find("select CONCAT(a.y_idea_name,'-',p.y_project_name) content,a.cdate start,a.y_idea_status status from t_y_idea a ,t_y_project p where 1=1\n" +
                " and a.y_project_id = p.id and a.cdate >current_date()-7");//and a.y_idea_status=10
    }

    public List<YIdeaModel> findRelaProject(Map<String,Object> param){
        return findRelaProject(null,param);
    }

    public List<YIdeaModel> findRelaProject(Map<String,Object[]> querytype,Map<String,Object> param){
        String sql="select a.*,(select count(1) from t_y_comment c where a.id = c.y_comment_srcid and c.y_comment_srctable = 't_y_idea') commentcount" +
                " from t_y_idea a ,t_y_project p where 1=1 and a.y_project_id = p.id ";
        SqlKit s = new SqlKit();
        if(param!=null && !param.isEmpty()){
            if(querytype != null){
                s.buildSQL(querytype,param);
            }else {
                s.buildSQL(param);
            }
            sql+= s.getSql();
        }
        sql = s.appendRightSql(sql,"p","order by a.y_idea_sort");
        Logger.getLogger(YIdeaModel.class).info(sql);
        return  dao.find(sql,s.getParamArray());
    }


}
